package com.viper.weather.logic.dao

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.google.gson.Gson
import com.viper.weather.MyApplication
import com.viper.weather.logic.model.Place

/**
 * Created by viper.
 * Time: 20-6-27 19:26:27
 * Description:
 */
object PlaceDao {
    fun savePlace(place: Place) {
        sharedPreferences().edit {
            putString("place", Gson().toJson(place))
        }
    }

    fun getSavedPlace(): Place {
        val placeJson = sharedPreferences().getString("place", "")
        return Gson().fromJson(placeJson, Place::class.java)
    }

    fun isPlaceSaved() = sharedPreferences().contains("place")

    private fun sharedPreferences() =
            MyApplication.context.getSharedPreferences("weather", Context.MODE_PRIVATE)
}